package com.mission.schedule.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.adapter.utils.CommonAdapter;
import com.mission.schedule.adapter.utils.ViewHolder;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetDaiBanActivity extends BaseActivity implements
        OnClickListener {

    @ViewResId(id = R.id.new_dtl_back)
    private LinearLayout new_dtl_back;
    @ViewResId(id = R.id.beforetime_lv)
    private ListView beforetime_lv;
    @ViewResId(id = R.id.top_banner_center)
    private TextView top_banner_center;


    private String beforetime;
    private int lastIndex;

    Context context;
    ChooseStateAdapter adapter = null;
    List<String> list = new ArrayList<String>();
    private Map<Integer, Boolean> isSelected;
    String type = "4";

    @Override
    protected void setListener() {
        new_dtl_back.setOnClickListener(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_setbeforetime);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        load();
        top_banner_center.setText("待办设置");
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        type = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.DBCOUNT_SHOW, "4");
        if (isSelected != null)
            isSelected = null;
        isSelected = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            isSelected.put(i, false);
        }

        if ("0".equals(type)) {
            lastIndex = 0;
        } else if ("2".equals(type)) {
            lastIndex = 1;
        } else if ("4".equals(type)) {
            lastIndex = 2;
        } else if ("6".equals(type)) {
            lastIndex = 3;
        } else if ("8".equals(type)) {
            lastIndex = 4;
        } else if ("10".equals(type)) {
            lastIndex = 5;
        }

    }

    @Override
    protected void setAdapter() {
        adapter = new ChooseStateAdapter(context, list, R.layout.adapter_choosestate, lastIndex);
        beforetime_lv.setAdapter(adapter);
        beforetime_lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter.notifyDataSetChanged();
        beforetime_lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.DBCOUNT_SHOW, list.get(position));
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_dtl_back:
                this.finish();
                break;

            default:
                break;
        }
    }

    public class ChooseStateAdapter extends CommonAdapter<String> {
        private List<String> mList;
        private int lastIndex;

        public ChooseStateAdapter(Context context, List<String> lDatas, int layoutItemID, int lastIndex) {
            super(context, lDatas, layoutItemID);
            this.mList = lDatas;
            this.lastIndex = lastIndex;
        }

        @Override
        public void getViewItem(ViewHolder holder, final String item, final int position) {
            TextView state_item_title = holder.getView(R.id.state_item_title);
            CheckBox state_item_cb = holder.getView(R.id.state_item_cb);

            state_item_title.setText(item);
            if (lastIndex == position) {
                state_item_cb.setChecked(true);
            } else {
                state_item_cb.setChecked(false);
            }

        }
    }

    SharedPrefUtil sharedPrefUtil;

    private void load() {
        list.add("0");
        list.add("2");
        list.add("4");
        list.add("6");
        list.add("8");
        list.add("10");
    }

}
